import 'package:scoped_model/scoped_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class CounterModel extends Model {
  int _counter = 0;

  int get counter => _counter;

  void increment() {
    _counter++;

    notifyListeners();
  }

  static CounterModel of(BuildContext context) =>
      ScopedModel.of<CounterModel>(context);
}

class CounterApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScopedModel<CounterModel>(
      model: CounterModel(),
      child: Scaffold(
          appBar: AppBar(
            title: Text("Scoped Model"),
            centerTitle: true,
          ),
          body: Column(
            children: <Widget>[
              ScopedModelDescendant<CounterModel>(
                builder: (context, child, model) => Text('${model.counter}'),
              ),
              Text("Another widget that doesn't depend on the CounterModel")
            ],
          ),
          floatingActionButton: ScopedModelDescendant<CounterModel>(
              builder: (context, child, model) {
            return FloatingActionButton(
              onPressed: () {
//                第一种获取model的方式
//                model.increment();
//                第二种获取model的方式
                CounterModel.of(context).increment();
              },
              child: ScopedModelDescendant<CounterModel>(
                builder: (context, child, model) => Text('${model.counter}'),
              ),
            );
          })),
    );
  }
}
